﻿using ClusterAwareAdmin;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClusterAwareProperties;
using System.Linq;

namespace ClusterAwareAdminTest
{
    
    
    /// <summary>
    ///This is a test class for GroupAdminTest and is intended
    ///to contain all GroupAdminTest Unit Tests
    ///</summary>
    [TestClass]
    public class GroupAdminTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        private static ClusterManagerAdmin _manager;


        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void ClassTestInitialize(TestContext testContext)
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Create(SharedConsts.NewGroupTest);
            }
            if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Create(SharedConsts.GroupTest);
            }
            if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
                managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
                managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
            }
            managerInit.Dispose();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void ClassTestCleanup()
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceDep);
            }
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.NewGroupTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.GroupTest);
            }
            managerInit.Dispose();
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void Initialize()
        {
            _manager = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            _manager.Connect();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void Cleanup()
        {
            _manager.Dispose();
        }
        //
        #endregion


        // <summary>
        //A test for SetType
        //</summary>
        //[TestMethod]
        //public void SetTypeTest()
        //{
        //    if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
        //    {
        //        Assert.Inconclusive("Windows 2003 not suport Method SetType");
        //    }
        //    _manager.CacheInstances = true;
        //    var target = new GroupAdmin(_manager);
        //    var grptype = target.GetGroup(SharedConsts.GroupTest).GroupType;
        //    target.SetType(SharedConsts.GroupTest, GroupType.FileServer);
        //    var actual = target.GetGroup(SharedConsts.GroupTest).GroupType;
        //    Assert.AreEqual(GroupType.FileServer,actual);
        //    target.SetType(SharedConsts.GroupTest,grptype);
        //    actual = target.GetGroup(SharedConsts.GroupTest).GroupType;
        //    Assert.AreEqual(grptype, actual);
        //    _manager.CacheInstances = false;
        //     grptype = target.GetGroup(SharedConsts.GroupTest).GroupType;
        //    target.SetType(SharedConsts.GroupTest, GroupType.FileServer);
        //     actual = target.GetGroup(SharedConsts.GroupTest).GroupType;
        //    Assert.AreEqual(GroupType.FileServer, actual);
        //    target.SetType(SharedConsts.GroupTest, grptype);
        //    actual = target.GetGroup(SharedConsts.GroupTest).GroupType;
        //    Assert.AreEqual(grptype, actual);
        //}

        /// <summary>
        ///A test for SetPreferredOwners
        ///</summary>
        [TestMethod]
        public void SetPreferredOwnersTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method SetPreferredOwners");
            }
            _manager.CacheInstances = true;
            var target = new GroupAdmin(_manager);
            var nodes = _manager.Nodes.GetNodes().Select(n => n.Name).ToArray();
            target.SetPreferredOwners(SharedConsts.GroupTest,nodes);
            Assert.AreNotEqual(0,_manager.Groups.GetPreferredNodes(SharedConsts.GroupTest).Length);
            target.SetPreferredOwners(SharedConsts.GroupTest, new string[]{});
            Assert.AreEqual(0, _manager.Groups.GetPreferredNodes(SharedConsts.GroupTest).Length);
            _manager.CacheInstances = false;
            nodes = _manager.Nodes.GetNodes().Select(n => n.Name).ToArray();
            target.SetPreferredOwners(SharedConsts.GroupTest, nodes);
            Assert.AreNotEqual(0, _manager.Groups.GetPreferredNodes(SharedConsts.GroupTest).Length);
            target.SetPreferredOwners(SharedConsts.GroupTest, new string[] { });
            Assert.AreEqual(0, _manager.Groups.GetPreferredNodes(SharedConsts.GroupTest).Length);
        }

        /// <summary>
        ///A test for Rename
        ///</summary>
        [TestMethod]
        public void RenameTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupAdmin(_manager);
            target.Rename(SharedConsts.GroupTest,SharedConsts.GroupTest + "Rename");
            Assert.AreNotEqual(null,target.GetGroup(SharedConsts.GroupTest + "Rename"));
            target.Rename(SharedConsts.GroupTest + "Rename", SharedConsts.GroupTest);
            Assert.AreNotEqual(null, target.GetGroup(SharedConsts.GroupTest));
            _manager.CacheInstances = false;
            target.Rename(SharedConsts.GroupTest, SharedConsts.GroupTest + "Rename");
            Assert.AreNotEqual(null, target.GetGroup(SharedConsts.GroupTest + "Rename"));
            target.Rename(SharedConsts.GroupTest + "Rename", SharedConsts.GroupTest);
            Assert.AreNotEqual(null, target.GetGroup(SharedConsts.GroupTest));
        }

        /// <summary>
        ///A test for Destroy
        ///</summary>
        [TestMethod]
        public void DestroyTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method Destroy");
            }
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new GroupAdmin(_manager);
            target.Destroy(SharedConsts.GroupTest);
            Assert.AreEqual(false, target.ExistGroup(SharedConsts.GroupTest));
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = false;
            target = new GroupAdmin(_manager);
            target.Destroy(SharedConsts.GroupTest);
            Assert.AreEqual(false, target.ExistGroup(SharedConsts.GroupTest));
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod]
        public void DeleteTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new GroupAdmin(_manager);
            target.Delete(SharedConsts.NewGroupTest);
            Assert.AreEqual(false, target.ExistGroup(SharedConsts.NewGroupTest));
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = false;
            target.Delete(SharedConsts.NewGroupTest);
            Assert.AreEqual(false, target.ExistGroup(SharedConsts.NewGroupTest));
        }

        /// <summary>
        ///A test for Create
        ///</summary>
        [TestMethod]
        public void CreateTest()
        {
            _manager.CacheInstances = true;
            var target = new GroupAdmin(_manager);
            if (target.ExistGroup(SharedConsts.NewGroupTest))
            {
                target.Delete(SharedConsts.NewGroupTest);
            }
            target.Create(SharedConsts.NewGroupTest);
            Assert.AreEqual(true, target.ExistGroup(SharedConsts.NewGroupTest));
            _manager.CacheInstances = false;
            if (target.ExistGroup(SharedConsts.NewGroupTest))
            {
                target.Delete(SharedConsts.NewGroupTest);
            }
            target.Create(SharedConsts.NewGroupTest);
            Assert.AreEqual(true, target.ExistGroup(SharedConsts.NewGroupTest));
        }

        /// <summary>
        ///A test for GroupAdmin Constructor
        ///</summary>
        [TestMethod]
        public void GroupAdminConstructorTest()
        {
           new GroupAdmin(_manager);
        }
    }
}
